import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import * as Auth from 'common/auth'

import Login from 'components/Login'
import Home from 'components/Home'
import NotFound from 'components/404'
import NoAuth from 'components/403'
// import MenuFolder from 'components/MenuFolder'
import RouterView from 'components/RouterView'

import Admins from 'pages/admins/List'
import AdminsEdit from 'pages/admins/Edit'
import Couriers from 'pages/couriers/List'
import CouriersEdit from 'pages/couriers/Edit'
import ShipmentsDetail from 'pages/shipments/ShipmentsDetail.vue'
import ShipmentsList from 'pages/shipments/ShipmentsList.vue'
import ShipmentsEdit from 'pages/shipments/ShipmentsEdit.vue'
import VersionList from 'pages/version/VersionList.vue'
import VersionEdit from 'pages/version/VersionEdit.vue'
import UsersList from 'pages/users/UsersList.vue'
import UsersReceiverList from 'pages/users/UsersReceiverList.vue'
import UsersReceiverEdit from 'pages/users/UsersReceiverEdit.vue'
import UsersDetail from 'pages/users/UsersDetail.vue'
import UsersEdit from 'pages/users/UsersEdit.vue'
import OrdersList from 'pages/orders/OrdersList'
import OrdersDetail from 'pages/orders/OrdersDetail.vue'
import TransactionsList from 'pages/transactions/TransactionsList.vue'
import TransactionsDetail from 'pages/transactions/TransactionsDetail.vue'
import StockLocationsList from 'pages/stock/StockLocationsList.vue'
import StockLocationsEdit from 'pages/stock/StockLocationsEdit.vue'
import StockItemsList from 'pages/stock/StockItemsList.vue'
import StockItemsEdit from 'pages/stock/StockItemsEdit.vue'
import HomeBannersList from 'pages/home/HomeBannersList.vue'
import HomeBannersEdit from 'pages/home/HomeBannersEdit.vue'
import HomeBannersNews from 'pages/home/HomeBannersNews.vue'
import HomeRecommendsList from 'pages/home/HomeRecommendsList.vue'
import HomeRecommendsEdit from 'pages/home/HomeRecommendsEdit.vue'
import ProductsList from 'pages/product/ProductsList.vue'
import ProductsEdit from 'pages/product/ProductsEdit.vue'
import ProductsDetails from 'pages/product/edit/ProductsDetails.vue'
import ProductsImages from 'pages/product/edit/ProductsImages.vue'
import ProductsImagesEdit from 'pages/product/edit/ProductsImagesEdit.vue'
import ProductsProperties from 'pages/product/edit/ProductsProperties.vue'
import ProductsPropertiesEdit from 'pages/product/edit/ProductsPropertiesEdit.vue'
import ProductsItems from 'pages/product/edit/ProductsItems.vue'
import ProductsItemsEdit from 'pages/product/edit/ProductsItemsEdit.vue'
import PropertyList from 'pages/product/PropertyList.vue'
import PropertyEdit from 'pages/product/PropertyEdit.vue'
import OptionTypeList from 'pages/product/OptionTypeList.vue'
import OptionTypeEdit from 'pages/product/OptionTypeEdit.vue'
import CategoriesList from 'pages/product/CategoriesList.vue'
import CustomerManagersList from 'pages/customer/CustomerManagersList.vue'
import CustomerEdit from 'pages/customer/CustomerEdit.vue'
import OltpTxnLogs from 'pages/report/OltpTxnLogs.vue'
import CleanTxnDetails from 'pages/report/CleanTxnDetails.vue'
import UserBalances from 'pages/report/UserBalances.vue'
import LoggersList from 'pages/loggers/LoggersList.vue'

// 路由统一定义
const routes = [
  {
    path: '/login',
    component: Login,
    meta: { name: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: Home,
    meta: { name: '首页' },
    children: [
      { path: '/', redirect: '/admins' },
      {
        path: '/admins',
        meta: { title: '用户管理' },
        component: RouterView,
        children: [
          { path: '/admins', component: Admins, meta: { title: '', permission: 'admins:list' } },
          { path: '/admins/new', component: AdminsEdit, meta: { title: '新增', permission: 'admins:add' } },
          { path: '/admins/:id/edit', component: AdminsEdit, meta: { title: '编辑', permission: 'admins:edit' } }
        ]
      },
      {
        path: '/users',
        meta: { title: '商户管理' },
        component: RouterView,
        children: [
          { path: '', component: UsersList, meta: { title: '', permission: 'users:list' } },
          { path: ':id', component: UsersDetail, meta: { title: '查看', permission: 'users:view' } },
          { path: ':id/edit', component: UsersEdit, meta: { title: '编辑', permission: 'users:edit' } },
          {
            path: ':id/receivers',
            meta: { title: '收货地址' },
            component: RouterView,
            children: [
              { path: '', component: UsersReceiverList, meta: { title: '', permission: 'users:list' } },
              { path: ':receiverId/edit', component: UsersReceiverEdit, meta: { title: '编辑', permission: 'users:edit' } },
              { path: 'new', component: UsersReceiverEdit, meta: { title: '新增', permission: 'users:add' } }
            ]
          }
        ]
      },
      {
        path: '/',
        meta: { title: '商品管理', folder: true },
        component: RouterView,
        children: [
          {
            path: '/products',
            meta: { title: '商品' },
            component: RouterView,
            children: [
              { path: '/products', component: ProductsList, meta: { title: '', permission: 'products:list' } },
              {
                path: '/products/:id',
                meta: { title: '编辑商品' },
                component: ProductsEdit,
                children: [
                  { path: '/products/:id', redirect: '/products/:id/edit' },
                  { path: '/products/:id/edit', component: ProductsDetails, meta: { title: '详情', permission: 'products:edit' } },
                  {
                    path: '/products/:id/images',
                    meta: { title: '图片' },
                    component: RouterView,
                    children: [
                      { path: '/products/:id/images', component: ProductsImages, meta: { title: '', permission: 'products:edit' } },
                      { path: '/products/:id/images/new', component: ProductsImagesEdit, meta: { title: '新增', permission: 'products:edit' } },
                      { path: '/products/:id/images/:image_id/edit', component: ProductsImagesEdit, meta: { title: '编辑', permission: 'products:edit' } }
                    ]
                  },
                  {
                    path: '/products/:id/product_properties',
                    meta: { title: '属性' },
                    component: RouterView,
                    children: [
                      { path: '/products/:id/product_properties', component: ProductsProperties, meta: { title: '', permission: 'products:edit' } },
                      { path: '/products/:id/product_properties/new', component: ProductsPropertiesEdit, meta: { title: '新增', permission: 'products:edit' } },
                      { path: '/products/:id/product_properties/:property_id/edit', component: ProductsPropertiesEdit, meta: { title: '编辑', permission: 'products:edit' } }
                    ]
                  },
                  {
                    path: '/products/:id/product_items',
                    meta: { title: '库存项' },
                    component: RouterView,
                    children: [
                      { path: '/products/:id/product_items', component: ProductsItems, meta: { title: '', permission: 'products:edit' } },
                      { path: '/products/:id/product_items/:product_item_id/edit', component: ProductsItemsEdit, meta: { title: '编辑', permission: 'products:edit' } },
                      { path: '/products/:id/product_items/new', component: ProductsItemsEdit, meta: { title: '新增', permission: 'products:edit' } }
                    ]
                  }
                ]
              }
            ]
          },
          {
            path: '/properties',
            meta: { title: '属性' },
            component: RouterView,
            children: [
              { path: '/properties', component: PropertyList, meta: { title: '', permission: 'properties:list' } },
              { path: '/properties/:id/edit', component: PropertyEdit, meta: { title: '编辑', permission: 'properties:edit' } },
              { path: '/properties/new', component: PropertyEdit, meta: { title: '新增', permission: 'properties:edit' } }
            ]
          },
          {
            path: '/options',
            meta: { title: '选项' },
            component: RouterView,
            children: [
              { path: '/options', component: OptionTypeList, meta: { title: '', permission: 'options:list' } },
              { path: '/options/:id/edit', component: OptionTypeEdit, meta: { title: '编辑', permission: 'options:list' } },
              { path: '/options/new', component: OptionTypeEdit, meta: { title: '新增', permission: 'options:list' } }
            ]
          },
          {
            path: '/categories',
            meta: { title: '分类' },
            component: RouterView,
            children: [
              { path: '/categories', component: CategoriesList, meta: { title: '', permission: 'categories:list' } }
            ]
          }
        ]
      },
      {
        path: '/customer_managers',
        meta: { title: '客户经理管理' },
        component: RouterView,
        children: [
          { path: '/customer_managers', component: CustomerManagersList, meta: { title: '', permission: 'customer_managers:list' } },
          { path: '/customer_managers/:id/edit', component: CustomerEdit, meta: { title: '编辑', permission: 'customer_managers:edit' } },
          { path: '/customer_managers/new', component: CustomerEdit, meta: { title: '新增', permission: 'customer_managers:edit' } }
        ]
      },
      {
        path: '/orders',
        meta: { title: '订单管理' },
        component: RouterView,
        children: [
          { path: '/orders', component: OrdersList, meta: { title: '', permission: 'orders:list' } },
          { path: '/orders/:id', component: OrdersDetail, meta: { title: '订单详情', permission: 'orders:view' } }
        ]
      },
      {
        path: '/',
        component: RouterView,
        meta: { title: '仓库管理', folder: true },
        children: [
          {
            path: '/stock_items',
            meta: { title: '库存' },
            component: RouterView,
            children: [
              { path: '/stock_items', component: StockItemsList, meta: { title: '', permission: 'stock_items:list' } },
              { path: '/stock_items/:id/edit', component: StockItemsEdit, meta: { title: '编辑', permission: 'stock_items:edit' } },
              { path: '/stock_items/new', component: StockItemsEdit, meta: { title: '新增', permission: 'stock_items:edit' } }
            ]
          },
          {
            path: '/stock_locations',
            meta: { title: '配销中心' },
            component: RouterView,
            children: [
              { path: '/stock_locations', component: StockLocationsList, meta: { title: '', permission: 'stock_locations:list' } },
              { path: '/stock_locations/:id/edit', component: StockLocationsEdit, meta: { title: '编辑', permission: 'stock_locations:edit' } },
              { path: '/stock_locations/new', component: StockLocationsEdit, meta: { title: '新增', permission: 'stock_locations:edit' } }
            ]
          }
        ]
      },
      {
        path: '/',
        component: RouterView,
        meta: { title: '物流管理', folder: true },
        children: [
          {
            path: '/couriers',
            meta: { title: '承运人' },
            component: RouterView,
            children: [
              { path: '/couriers', component: Couriers, meta: { title: '', requiresAuth: true, permission: 'couriers:list' } },
              { path: '/couriers/new', component: CouriersEdit, meta: { title: '新增', permission: 'couriers:add' } },
              { path: '/couriers/:id/edit', component: CouriersEdit, meta: { title: '编辑', permission: 'couriers:edit' } }
            ]
          },
          {
            path: '/shipments',
            meta: { title: '送货单' },
            component: RouterView,
            children: [
              { path: '/shipments', component: ShipmentsList, meta: { title: '', permission: 'shipments:list' } },
              { path: '/shipments/:id', component: ShipmentsDetail, meta: { title: '详情', permission: 'shipments:view' } },
              { path: '/shipments/:id/edit', component: ShipmentsEdit, meta: { title: '编辑', permission: 'shipments:edit' } }
            ]
          }
        ]
      },
      {
        path: '/',
        component: RouterView,
        meta: { title: '首页配置', folder: true },
        children: [
          {
            path: '/home_banners',
            meta: { title: '轮播内容' },
            component: RouterView,
            children: [
              { path: '/home_banners', component: HomeBannersList, meta: { title: '', permission: 'home_banners:list' } },
              { path: '/home_banners/:id/edit', component: HomeBannersEdit, meta: { title: '编辑', permission: 'home_banners:edit' } },
              { path: '/home_banners/new/news/banner_type', component: HomeBannersNews, meta: { title: '添加新闻', permission: 'home_banners:edit' } }
            ]
          },
          {
            path: '/home_recommends',
            meta: { title: '推荐内容' },
            component: RouterView,
            children: [
              { path: '/home_recommends', component: HomeRecommendsList, meta: { title: '', permission: 'home_recommends:list' } },
              { path: '/home_recommends/:id/edit', component: HomeRecommendsEdit, meta: { title: '编辑', permission: 'home_recommends:edit' } }
            ]
          }
        ]
      },
      {
        path: '/transactions',
        meta: { title: '资金流水' },
        component: RouterView,
        children: [
          { path: '/transactions', name: '', component: TransactionsList, meta: { title: '', permission: '' } },
          { path: '/transactions/:id', name: '详情', component: TransactionsDetail, meta: { title: '详情', permission: '' } }
        ]
      },
      {
        path: '/version_infos',
        meta: { title: '版本信息' },
        component: RouterView,
        children: [
          { path: '/version_infos', component: VersionList, meta: { title: '', permission: 'version_infos:list' } },
          { path: '/version_infos/:id/edit', component: VersionEdit, meta: { title: '编辑', permission: 'version_infos:edit' } },
          { path: '/version_infos/new', component: VersionEdit, meta: { title: '新增', permission: 'version_infos:add' } }
        ]
      },
      { path: 'properties', component: PropertyList, meta: { permission: 'properties:list' } },
      { path: 'properties/:id/edit', component: PropertyEdit, meta: { permission: 'properties:edit' } },
      { path: 'properties/new', component: PropertyEdit, meta: { permission: 'properties:edit' } },
      { path: 'options', component: OptionTypeList, meta: { permission: 'options:list' } },
      { path: 'options/:id/edit', component: OptionTypeEdit, meta: { permission: 'options:list' } },
      { path: 'options/new', component: OptionTypeEdit, meta: { permission: 'options:list' } },
      { path: 'categories', component: CategoriesList, meta: { permission: 'categories:list' } },
      { path: 'customer_managers', component: CustomerManagersList, meta: { permission: 'customer_managers:list' } },
      { path: 'customer_managers/:id/edit', component: CustomerEdit, meta: { permission: 'customer_managers:edit' } },
      { path: 'customer_managers/new', component: CustomerEdit, meta: { permission: 'customer_managers:edit' } },
      { path: 'oltp_txn_logs', component: OltpTxnLogs, meta: { permission: 'oltp_txn_logs:list' } },
      { path: 'clean_txn_logs', component: CleanTxnDetails, meta: { permission: 'clean_txn_logs:list' } },
      { path: 'user_balances', component: UserBalances, meta: { permission: 'user_balances:list' } },
      { path: 'loggers', component: LoggersList, meta: { permission: 'loggers:list' } },
      // system routes start
      { path: '403', component: NoAuth },
      // if no matched, show 404 page
      { path: '*', component: NotFound }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log(`requireAuth=${to.meta.requiresAuth}, permission=${to.meta.permission}`)

  // anonymous ==> next
  if (to.meta.requiresAuth === false) {
    next()
    return
  }
  // need login adn no login ==> login page
  if (!Auth.hasLogin()) {
    next('/login')
    return
  }
  // need auth and no auth ==> 403 page
  if (!Auth.hasPermission(to.meta.permission)) {
    next('/403')
    return
  }
  next()
})

router.afterEach(transition => {

})

export { router }
